找传奇、传世资源到传世资源站!

C#实现控件拖动

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C#实现控件拖动 C#语言基础-第1张    public  class MoveControl
    {
        #region Constructors
        public MoveControl(Control ctrl,Form1 frm)
        {
            currentControl = ctrl;
            AddEvents();
        }
        #endregion        #region Fields
        private Control currentControl; //传入的控件
        private Point pPoint; //上个鼠标坐标
        private Point cPoint; //当前鼠标坐标
        FrameControl fc;//边框控件
        private Form1 form1 = null;        #endregion        #region Properties        #endregion        #region Methods
        /// <summary>
        /// 挂载事件
        /// </summary>
        private void AddEvents()
        {
            currentControl.MouseClick = new MouseEventHandler(MouseClick);
            currentControl.MouseDown = new MouseEventHandler(MouseDown);
            currentControl.MouseMove = new MouseEventHandler(MouseMove);
            currentControl.MouseUp = new MouseEventHandler(MouseUp);
        }        /// <summary>
        /// 绘制拖拉时的黑色边框
        /// </summary>
        public static void DrawDragBound(Control ctrl)
        {
            ctrl.Refresh();
            Graphics g = ctrl.CreateGraphics();
            int width = ctrl.Width;
            int height = ctrl.Height;
            Point[] ps = new Point[5]{new Point(0,0),new Point(width -1,0),
                new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
            g.DrawLines(new Pen(Color.Black), ps);
        }           public  void RemoveFc()
        {
            currentControl = null;
            if (fc != null)
            {
                fc.Visible = false;
                fc.Dispose();
            }
        }        #endregion        #region Events
        /// <summary>
        /// 鼠标单击事件:用来显示边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MouseClick(object sender, MouseEventArgs e)
        {
            this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
            this.currentControl.BringToFront();
            fc = new FrameControl(this.currentControl);
            this.currentControl.Parent.Controls.Add(fc);
            fc.Visible = true;
            fc.Draw();
            form1.moveControl = this;
        }        /// <summary>
        /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
        /// </summary>
        void MouseDown(object sender, MouseEventArgs e)
        {           
            pPoint = Cursor.Position;              
        }        /// <summary>
        /// 鼠标移动事件:让控件跟着鼠标移动
        /// </summary>
        void MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
            //当鼠标左键按下时才触发
            if (e.Button == MouseButtons.Left)
            {
                MoveControl.DrawDragBound(this.currentControl);
                if(fc != null ) fc.Visible = false; //先隐藏
                cPoint = Cursor.Position;//获得当前鼠标位置
                int x = cPoint.X - pPoint.X;
                int y = cPoint.Y - pPoint.Y;
                currentControl.Location = new Point(currentControl.Location.X x, currentControl.Location.Y y);
                pPoint = cPoint;
               
                Panel cr = (Panel)this.currentControl.Parent;//zxl
                Rectangle s = new Rectangle(1, 2, 1, 2);
                cr.Invalidate(s);
                cr.Update();
               
            }
        }        /// <summary>
        /// 鼠标弹起事件:让自定义的边框出现
        /// </summary>
        void MouseUp(object sender, MouseEventArgs e)
        {
            this.currentControl.Refresh();
            if (fc != null)
            {
                fc.Visible = true;
                fc.Draw();
            }
        }
        #endregion
    }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复